﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;

namespace EmployeeCheckIn
{
    public partial class EditTime : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["EditTime"] == null)
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);
                }
                else
                {
                    string emp = Session["EditTime"].ToString();
                    SerchDB timest = new SerchDB();
                    DataTable dt = timest.timestart();
                    CutString newCut = new CutString();
                    TextBox1.Text = newCut.cutEMPCODE(emp) + " -   " + newCut.cutFirsName(emp) + "  " + newCut.cutLastName(emp);
                    TextBox2.Text = Convert.ToDateTime(newCut.cutDate(emp)).ToString("dd/MM/yyyy");
                    TextBox3.Text = newCut.cutCheckIn(emp);
                    TextBox4.Text = newCut.cutCheckOut(emp);
                    HiddenField1.Value = newCut.cutEMP(emp);
                    HiddenField2.Value = newCut.cutCheckIn(emp);
                    HiddenField3.Value = dt.Rows[0]["TIME_OF_START"].ToString();
                    
                }

            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null && Session["Pass"] != null)
            {
                CutString newCut = new CutString();
                DateTime BeforTime = newCut.dtm(TextBox2.Text, HiddenField2.Value);
                DateTime AfterTime = newCut.dtm(TextBox2.Text, TextBox3.Text);
                DateTime StartTime = newCut.dtm(TextBox2.Text, HiddenField3.Value);
                DateTime CheckOutEd = newCut.dtm(TextBox2.Text, TextBox4.Text);
                InSertData EditAllTbCheckin = new InSertData();
                EditTimeUpdate ed = new EditTimeUpdate();
                int LateTime = ed.ReLateTime(AfterTime, StartTime);
                EditAllTbCheckin.UpdateTime(Convert.ToInt32(HiddenField1.Value), AfterTime, CheckOutEd, LateTime, BeforTime);
                Session["editReload"] = "1";
                Response.Write("<script>alert('แก้ใขข้อมูลเรียบร้อยแล้ว');window.close(); if (window.opener && !window.opener.closed) { window.opener.location.reload();} </script>");
                // this.ClientScript.RegisterClientScriptBlock(this.GetType(), "Close", "window.close()", true);
            }
            else
            {
                Response.Redirect("Default.aspx");
            }
        }
    }
}